package auth

import (
	"data/view/auth/auth_const"
	"data/view/user"
	"plug/myauth"
	"strconv"
)

//通过accesstoken获取用户是否有权限
func CheckRoleAuthByToken(access_token string, module string, levels ...string) bool {
	u, bb := user.GetUserDetailFromToken(access_token)
	if !bb {
		return false
	}
	//权限检验
	return myauth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_A, auth_const.L_C)
}

//通过username获取用户是否有权限
func CheckRoleAuthByUsername(username string, module string, levels ...string) bool {
	u, bb := user.GetUserDetail(username)
	if !bb {
		return false
	}

	//权限检验
	return myauth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_A, auth_const.L_C)
}

//多角色权限判断（不支持topic/bundle/pid）
func CheckRoleAuth(rolestr string, module string, levels ...string) bool {
	return myauth.CheckRoleAuth(rolestr, module, levels...)
}

//拉取全部权限
func OnGetAllAuth() (result []AuthResult) {
	auths := myauth.GetAuthConfigDetailInfo() //已有权限
	//全部角色
	// var roles []user.User_role_tbl
	// orm.Find(&roles)
	roles := user.GetRoleConfigInfo()

	if len(roles) > 0 {
		for _, v := range roles {
			var tmp AuthResult
			var tt []myauth.Auth_info
			tmp.Role = v.Role
			tmp.Role_name = v.Desc

			for k, n := range auth_const.ModulMap {
				var t myauth.Auth_info
				t.Module = k
				t.Module_name = n
				t.Auth_level = auths[strconv.Itoa(v.Role)+"_"+k].Auth_level
				t.Des = auths[strconv.Itoa(v.Role)+"_"+k].Des
				tt = append(tt, t)
			}

			tmp.Auths = tt
			result = append(result, tmp)
		}
	}
	return
}
